#include "wordPattern.h"
std::vector<std::string> split(std::string &s, const int ch) {
    const int n = s.size();
    int start = 0;
    int end = 0;
    std::vector<std::string> result;
    for (; end < n; end++) {
        if (s[end] == ch) {
            result.push_back(s.substr(start, end - start));
            start = end + 1;
        }
    }
    result.push_back(s.substr(start, end - start));
    return result;
}
bool wordPattern(std::string pattern, std::string s) {
    std::vector<std::string> vecs = split(s);
    std::vector<int> index_pattern;
    std::unordered_map<char, std::string> map;
    bool flag = true;

    if (pattern.size() != vecs.size())
        return false;

    for (int i = 0; i < pattern.size(); i++) {
        if (map.count(pattern[i])) {
            if (map[pattern[i]] != vecs[i]) {
                flag = false;
                break;
            }
        } else {
            map[pattern[i]] = vecs[i];
        }
    }
    std::set<std::string> rep;
    for (auto e : map) {
        rep.insert(e.second);
    }
    if (rep.size() != map.size())
        flag = false;

    return flag;
}
